<template>
	<view v-if="orderInfo.orders">
		<!-- :style="{height: (device.windowHeight-device.footerHeight)+'px'}" -->
		<scroll-view scroll-y class="shop" :style="{'height': (device.windowHeight-device.footerHeight)+'px'}">
			<view class="shop_item" v-for="(i,iShop) in orderInfo.orders" :key="i.id">
				<view class="goods" v-for="(item, index) in i.detailList" :key="index">
					<view class="goods_info">
						<view class="goods_img">
							<x-image :src="`${imageUrl}${item.picturePath}`"></x-image>
						</view>
						<view class="goods_data">
							<!-- <view class="goods_title">
								<u--text size="12" :lines="2" :text="item.goodsName"></u--text>
							</view> -->
							<view class="goods_title">{{item.goodsName}}</view>
							<view class="goods-unit" v-if="item.unit && item.unit.length>0">单位:{{item.unit}}</view>
							<view class="goods_num">
								<view class="goods_price">
									<view>￥{{ item.goodsPrice }}</view>
								</view>
								<view>x{{item.quantity}}</view>
							</view>
						</view>
					</view>
				</view>
				<view class="youhui" v-if="orderInfo.orders[0].status === 10">预计到手价￥<text
						class="text_red">{{ (orderInfo.orders[0].paidAmount-coupon.couponMoney).toFixed(2) }}</text>
				</view>
			</view>

			<x-address v-if="address" :address="address" :mode="'preview'"></x-address>

			<view class="shop_item">
				<view class="input">
					<view>订单编号</view>
					<view>{{orderInfo.orders[0].orderNumber}}</view>
				</view>
				<view class="input">
					<view>下单时间</view>
					<view>{{orderInfo.orders[0].createTime}}</view>
				</view>
				<view class="input">
					<view>支付方式</view>
					<view>微信支付</view>
				</view>
			</view>
			<view class="shop_item">
				<view class="input">
					<view>商品总价</view>
					<view>￥{{orderInfo.orders[0].meetAmount}}</view>
				</view>
				<view v-if="orderInfo.orders[0].couponId.length>0 && orderInfo.orders[0].couponAmount>0" class="input">
					<view>优惠劵</view>
					<view>￥{{orderInfo.orders[0].couponAmount}}</view>
				</view>
				<view class="input">
					<view>折后金额</view>
					<view v-if="orderInfo.orders[0].couponId.length>0 && orderInfo.orders[0].couponAmount>0">
						￥{{((orderInfo.orders[0].meetAmount-orderInfo.orders[0].couponAmount)*(orderInfo.orders[0].discountRate/10)).toFixed(2)}}
					</view>
					<view v-else>
						￥{{((orderInfo.orders[0].meetAmount)*(orderInfo.orders[0].discountRate/10)).toFixed(2)}}</view>
				</view>
				<view class="input">
					<view v-if="orderInfo.orders[0].status == 10||orderInfo.orders[0].status == 50">应付金额</view>
					<view v-else>实付金额</view>
					<view>￥{{ (orderInfo.orders[0].paidAmount).toFixed(2) }}</view>
				</view>
			</view>

			<view class="shop_item" v-if="orderInfo.invoiceId!=''">
				<view class="input">
					<view>发票</view>
					<view class="picker" @click="handleShowInvoice">
						<view>
							{{orderInfo.orders && orderInfo.orders.length>0 && orderInfo.orders[0].invoiceTitle && orderInfo.orders[0].invoiceTitle.length>0 ? orderInfo.orders[0].invoiceTitle : '选择发票类型'}}
						</view>
						<u-icon name="arrow-right" color="xIconColor()" size="10"></u-icon>
					</view>
				</view>
			</view>

			<u-popup :show="showInvoice" mode="bottom" round="5">
				<view class="alert">
					<view class="user_name">已申请开票</view>
					<view class="alert_text">
						{{invoice.length>0?'发票已开具长按弹出菜单-》保存图片':'申请中'}}
					</view>
					<view class="cell no_border">
						<view class="cell_right">
							<view class="placeholder">
								<view v-for="(item, index) in invoice">
									<u-grid :border="false" col="1">
										<u-grid-item v-for="(item2,index2) in item.files" :key="item2">
											<u-image @click="imgListPreview(`${imageUrl}${item2.filePath}`)"
												:src="`${imageUrl}${item2.filePath}`" mode="widthFix" :lazy-load="true">
											</u-image>
										</u-grid-item>
									</u-grid>
								</view>
							</view>
						</view>
					</view>
					<view class="ic_close" @click="showInvoice = false"><u-icon name="close-circle" size="28"></u-icon>
					</view>
				</view>
				<x-footer>
					<button class="btn_enter x-btn" @click="subInvoice">关闭</button>
				</x-footer>
			</u-popup>

			<view class="shop_item" v-if="orderInfo.orders[0].status==40||orderInfo.orders[0].status==30">
				<view class="input">
					<view>配送方式</view>
					<view>{{orderInfo.orders[0].pickingType==1?orderInfo.orders[0].deliveryName:'自取'}}</view>
				</view>
				<view class="input" v-if="orderInfo.orders[0].pickingType==1">
					<view>快递单号</view>
					<view>{{orderInfo.orders[0].pickingCode}}</view>
				</view>
			</view>

			<view class="x-page-bottom-line"></view>
		</scroll-view>

		<x-footer>
			<view style="display: flex;flex-direction: column;">
				<u-text size="20" prefixIcon="￥" iconStyle="font-size: 12px;color:red" color="red"
					:text="orderInfo.orders[0].paidAmount-coupon.couponMoney"></u-text>
				<text
					style="font-size: 12px;color: #909399;">共优惠￥{{ (orderInfo.orders[0].preferentialAmount).toFixed(2) }}</text>
			</view>
		</x-footer>
	</view>
</template>

<script>
	import {
		pay,
		getInfo,
		addOrUpdate
	} from '@/api/order.js';
	import {
		getInvoiceByOrderNum
	} from '@/api/invoice.js';
	import {
		getAddress
	} from '@/api/address.js';
	import config from '@/config';
	import {
		getShop
	} from '@/api/shop.js'
	import {
		mapState
	} from 'vuex'
	import XImage from '@/components/image/xImage.vue'
	import XAddress from '@/pages/components/address/xAddress.vue'

	export default {
		components: {
			XImage,
			XAddress,
		},
		computed: {
			...mapState(['device'])
		},
		data() {
			return {
				invoice: [],
				showInvoice: false,
				showCoupon: false,
				// device: this.$store.state.device,
				orderInfo: {
					pickingType: 1
				},
				coupon: {
					couponMoney: 0,
					id: ''
				},
				couponList: [],
				imageUrl: config.imageUrl, //
				address: null, //字典
				isCar: false,
				shopList: [],
				total: 0,
				isCar: 0,
				shopName: '',
				totalAmount: 0,
			};
		},
		// onShow() {
		// 	if(uni.getStorageSync('invoice')){
		// 		this.invoice = uni.getStorageSync('invoice');
		// 	}
		// },
		// onHide() {
		// 	uni.setStorageSync('invoice', '');

		// },
		onLoad(options) {
			this.isCar = options.isCar;
			if (options.checkedList) {
				this.checkedList = JSON.parse(options.checkedList)
			}
			this.orderInfo = JSON.parse(options.info);

			//<view class="address x-text-multi">
			//	{{getAddressDesc(address.addressName, address.addressDetail)}}
			//</view>
			// <view class="detail">
			// 	<span class="default" v-if="address.defaultAddress">默认</span>
			// 	<span class="name"
			// 		v-if="address.receiptName && address.receiptName.length>0">{{address.receiptName}}</span>
			// 	<span class="contact" v-if="address.mobile && address.mobile.length>0">{{address.mobile}}</span>
			// </view>
			console.log("this.orderInfo=", this.orderInfo)
			if (this.orderInfo?.orders?.length > 0) {
				let address = {};
				address.addressName = this.orderInfo.orders[0].address
				address.addressDetail = this.orderInfo.orders[0].addressDetail
				address.receiptName = this.orderInfo.orders[0].receiptName
				address.mobile = this.orderInfo.orders[0].mobile
				this.address = address
				console.log("this.address=", this.address)
			}


			this.totalAmount = options.totalAmount
			// this.getShops()
			console.log("this.orderInfo=", this.orderInfo)
		},
		methods: {
			handleShowInvoice() {
				getInvoiceByOrderNum(this.orderInfo.orders[0].orderNumber).then(res => {
					this.invoice = res
				})
				this.showInvoice = true
			},
			close() {

			},
			open() {

			},
			subInvoice() {
				this.showInvoice = false;
				// addOrUpdate(this.orderInfo).then(res=>{
				// 	this.showInvoice = false;
				// })
			},
			imgListPreview(url) {
				var urlList = []
				urlList.push(url) //push中的参数为 :src="item.img_url" 中的图片地址
				uni.previewImage({
					indicator: "number",
					loop: true,
					urls: urlList
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	@import '@/mixins/theme/xTheme.scss';
    $secondary-color: #999;
	.cell {
		display: flex;
		justify-content: space-between;
	}

	.cell_right {
		display: flex;
		align-items: center;
		text-align: right;
		min-width: 200rpx;
		justify-content: flex-end;

		.cell_input {
			margin-right: 10rpx;
		}
	}


	.youhui {
		font-size: 26rpx;
		margin-top: 10rpx;
		margin: 15rpx;
		text-align: right;
		padding-bottom: 20rpx;
	}

	.first_gift_text {
		border: 1rpx solid #e1251b;
		color: #e1251b;
		font-size: 24rpx;
		padding: 2rpx 4rpx;
		display: inline-block;
		margin-top: 4rpx;
	}

	.alert {
		padding: 40rpx 20rpx 140rpx;
		position: relative;

		.ic_close {
			position: absolute;
			top: 20rpx;
			right: 20rpx;
		}

		.user_name {
			padding: 0 20rpx;
		}

		.alert_text {
			color: #515151;
			font-size: 28rpx;
			margin: 40rpx auto;
			padding: 0 20rpx;
		}

		.btn_enter {
			width: 100%;
			height: 90rpx;
			margin: 0 auto;
			border-radius: 6rpx;
		}

		.coupon_list {
			height: 50vh;
			width: 100%;
			overflow-y: scroll;
			margin: 30rpx auto;

			.item {
				width: 690rpx;
				height: 200rpx;
				background-color: #fff;
				border-radius: 6rpx;
				box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.3);
				margin: 0 auto 20rpx;
				display: flex;
				align-items: center;
				overflow: hidden;
				justify-content: space-between;
				padding-right: 30rpx;

				.item_left {
					width: 200rpx;
					height: 300rpx;
					background-color: #e1251b;
					border-top-right-radius: 100%;
					border-bottom-right-radius: 100%;
					display: flex;
					align-items: center;
					justify-content: center;
					flex-direction: column;
					color: #fff;
					font-size: 30rpx;
				}

				.item_price {
					font-size: 50rpx;
					font-weight: bold;
				}

				.item_content {
					font-size: 26rpx;
					color: #d5d5d5;
					margin-left: 20rpx;
				}

				.item_title {
					font-size: 32rpx;
					color: #333;
				}
			}
		}
	}

	.goods_img {
		width: 180rpx;
		border-radius: 10rpx;
		margin-right: 20rpx;
		height: 100%;
		overflow: hidden;
	}

	.goods {
		height: 220rpx;
		border-radius: 10rpx;
		display: flex;
		align-items: center;
		padding: 20rpx;
	}

	.goods_info {
		display: flex;
		align-items: center;
		height: 100%;
	}

	.goods_img>image {
		width: 100%;
	}

	.goods_info>.goods_data {
		width: 480rpx;
		height: 100%;
		display: flex;
		justify-content: space-between;
		flex-direction: column;
	}

	.goods_title {
		// font-size: 30rpx;
		// overflow: hidden;
		// text-overflow: ellipsis;
		// display: -webkit-box;
		// -webkit-box-orient: vertical;
		// -webkit-line-clamp: 2;
		// padding-top: 10rpx;
		font-size: 30rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		padding-top: 10rpx;
		font-weight: bold;
	}

	.goods-unit {
		margin-top: 10rpx;
		font-weight: 400;
		font-size: 28rpx;
		color: $secondary-color;
	}

	.goods_num {
		display: flex;
		justify-content: space-between;
		align-items: left;
	}

	.goods_price {
		font-size: 32rpx;
		font-weight: bold;
		color: #e51c23;
	}

	.shop_name {
		display: flex;
		align-items: center;
	}

	.shop_name>image {
		width: 36rpx;
		height: 36rpx;
		margin-right: 10rpx;
	}

	.shop {
		width: 100%;
		// padding: 6px 10px;
		background-color: $x-content-background;
		// margin: 20rpx auto;
		border-top: 1px solid #F5F5F5;
	}

	.shop_item {
		margin: 20rpx 20rpx 0;
		background-color: #fff;
		border-radius: 10rpx;
	}

	.gift {
		heigh: 180rpx;
	}

	.gift image {
		height: 180rpx;
	}

	// page {
	// 	padding-bottom: 90rpx;
	// }

	.text_red {
		color: #e51c23;
		font-weight: bold;
		font-size: 32rpx;
	}

	.address_none {
		height: 120rpx;
		border-bottom: 2rpx dashed #515151;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;

		&>text {
			font-size: 40rpx;
			font-weight: bold;
			margin-right: 20rpx;
		}

		.ic_right {
			width: 36rpx;
			height: 36rpx;
			position: absolute;
			right: 40rpx;
			top: calc(50% - 18rpx);
		}
	}

	.ic_right_picker {
		width: 36rpx;
		height: 36rpx;
		margin-left: 10rpx;
	}

	.border {
		width: 100%;
		height: 20rpx;
		background-color: #f0f0f0;
	}

	.input {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 20rpx;
		height: 90rpx;
		font-size: 28rpx;
		// border-bottom: 1rpx solid rgba(0, 0, 0, 0.1);

		.picker {
			display: flex;
			align-items: center;

			&>image {
				width: 36rpx;
				height: 36rpx;
				font-size: 28rpx;
				margin-left: 10rpx;
			}

			.placeholder {
				color: #707070;
			}
		}

		&>input {
			width: 440rpx;
			font-size: 28rpx;
			text-align: right;
		}
	}

	.order_info {
		font-size: 28rpx;
		height: 90rpx;
		text-align: right;
		line-height: 90rpx;
		padding: 0 20rpx;
	}

	.footer {
		width: 100%;
		height: 90rpx;
		background-color: #fff;
		position: fixed;
		z-index: 1;
		display: flex;
		padding: 0 20rpx;
		align-items: center;
		justify-content: space-between;
		border-top: 2rpx solid rgba(0, 0, 0, 0.1);

		.btn_sub {
			width: 24%;
			display: flex;
			background-color: #e51c23;
			color: #fff;
			align-items: center;
			justify-content: center;
			border-radius: 100rpx;
			height: 64rpx;
		}
	}

	.check_box {
		width: 40rpx;
		height: 40rpx;
		border-radius: 40rpx;
		border: 2rpx solid #909399;
		margin-right: 20rpx;
		display: flex;
	}

	.checked {
		background-color: #e51c23;
		border-color: #e51c23;
	}

	.check_box>image {
		width: 32rpx;
		height: 32rpx;
		margin: auto;
	}

	.default {
		display: flex;
		justify-content: center;
		color: white;
		background-color: red;
		border-radius: 5px;
		font-size: 12px;
		padding: 4rpx;
	}

	.alert {
		padding: 40rpx 20rpx 140rpx;
		position: relative;

		.ic_close {
			position: absolute;
			top: 20rpx;
			right: 20rpx;
		}

		.user_name {
			padding: 0 20rpx;
		}

		.alert_text {
			color: #515151;
			font-size: 28rpx;
			margin: 40rpx auto;
			padding: 0 20rpx;
		}

		.btn_enter {
			width: 100%;
			height: 90rpx;
			border-radius: 6rpx;
		}
	}

	.btn_enter {
		width: 100%;
		height: 90rpx;
		border-radius: 6rpx;
	}

	.placeholder {
		color: #808080;
	}
</style>